<template>
    <el-dialog v-model="props.visible" title="预付款退款申请" @close="handleClose">
        <el-form :model="formData" ref="form">
            <el-row :gutter="50">
                <el-col :span="12">
                    <el-form-item label="客户名称" prop="customerName">
                        {{ props.data?.customerName }}
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="级别" prop="operator">
                        {{ props.data?.customerLevel }}
                    </el-form-item>
                </el-col>
            </el-row>
            <el-form-item label="预付款金额">
                {{ props.data?.prePay }}
            </el-form-item>
            <el-form-item label="申请退款金额" prop="amount">
                <el-input v-model="formData.amount" placeholder="请输入"></el-input>
            </el-form-item>
            <el-form-item label="申请退款方式">
                <el-upload class="avatar-uploader"
                    ref="uploadRef"
                    :limit="1"
                    :action="uploadImgUrl"
                    :headers="headers"
                    :before-upload="beforeAvatarUpload" 
                    :on-success="handleAvatarSuccess"
                >
                    <img v-if="formData.proof" :src="formData.proof" class="avatar" />
                    <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
                </el-upload>
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button  type="primary" >确 定</el-button>
                <el-button @click="handleClose">取 消</el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import type { FormInstance,UploadInstance, UploadProps } from 'element-plus';
import { globalHeaders } from "@/utils/request";

const form = ref<FormInstance>();
const props = defineProps(['visible','data']);
const uploadRef = ref<UploadInstance>();
const baseUrl = import.meta.env.VITE_APP_BASE_API;
const uploadImgUrl = ref(baseUrl + "/resource/oss/upload");
const headers = ref(globalHeaders);
const fileType = ['image/jpeg','image/png'];

const emit = defineEmits(['submit','close']);


const formData = ref<any>({
    amount: undefined,
    proof: undefined,
    reason: undefined
});

const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
    if ( !fileType.includes(rawFile.type)) {
        ElMessage.error('图片必须为jpg或png格式!')
        return false;
    } else if (rawFile.size / 1024 / 1024 > 15) {
        ElMessage.error('图片大小不可超过 15MB!')
        return false;
    }
    return true;
};

const handleAvatarSuccess: UploadProps['onSuccess'] = (
    response
    ) => {
        formData.value.proof = response.data.url;
};

const handleClose = ()=>{
    form.value?.resetFields();
    emit('close');
};

</script>

<style scoped lang="scss">
.avatar-uploader .el-upload {
    border: 2px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
    border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    text-align: center;
}
.avatar{
    height: 150px;
    width: 150px;
}
</style>